package com.lang8.util;

import java.security.MessageDigest;
import java.util.UUID;

public class CodeUtil {
	public static String convertToMd5(String str){
		if ( str == null ){
			return null;
		}
		byte newByte1[] = str.getBytes();
		try{
			MessageDigest messagedigest = MessageDigest.getInstance("MD5");
			byte newByte2[] = messagedigest.digest(newByte1);
			String cryptograph = "";
			for(int i=0; i<newByte2.length; i++){
				String temp = Integer.toHexString( newByte2[i] & 0x000000ff );
				if( temp.length()<2 ) temp = "0"+temp;
				cryptograph += temp;
			}
			return cryptograph;
		}
		catch(Exception e){
			System.out.println("[ "+new java.util.Date().toString()+" ] [ CodeUtil.convertToMd5 ] "+e.toString());
			return null;
		}
	}
	public static String convertToSHA1(String str){
		if ( str == null ){
			return null;
		}
		byte newByte1[] = str.getBytes();
		try{
			MessageDigest messagedigest = MessageDigest.getInstance("SHA-1");
			byte newByte2[] = messagedigest.digest(newByte1);
			String cryptograph = "";
			for(int i=0; i<newByte2.length; i++){
				String temp = Integer.toHexString( newByte2[i] & 0x000000ff );
				if( temp.length()<2 ) temp = "0"+temp;
				cryptograph += temp;
			}
			return cryptograph;
		}
		catch(Exception e){
			System.out.println("[ "+new java.util.Date().toString()+" ] [ CodeUtil.convertToSHA1 ] "+e.toString());
			return null;
		}
	}
	public static String getUUID(){
		return UUID.randomUUID().toString();
	}
	public static void main(String[] args) {
		String uuid = getUUID();
		System.out.println(uuid);
//		String str = "abcdefdswefsefdgefda";
//		System.out.println(convertToMd5(str));
//		System.out.println(convertToSHA1(str));
	}
}
